/**
 * 
 */
package com.et114.modules.products.netbook.services;

import java.util.List;

import org.springframework.util.Assert;

import com.et114.components.simpletable.DataList;
import com.et114.modules.products.base.ProImage;
import com.et114.modules.products.commons.Const;
import com.et114.modules.products.commons.Context;
import com.et114.modules.products.netbook.dao.NetBookDao;
import com.et114.modules.products.netbook.vo.NetBook;
import com.et114.modules.products.proservices.ProService;
import com.et114.modules.products.search.SearchService;
import com.et114.modules.products.search.SearchVo;

/*
 * @author guanhw
 */
public class NetBookServiceImpl {
	private NetBookDao netBookDao ;
	private SearchService   searchCompass ; 

	public void setNetBookDao ( NetBookDao netBookDao ) {
		this.netBookDao = netBookDao;
	} 
	
	public void setSearchCompass ( SearchService searchCompass ) {
		this.searchCompass = searchCompass;
	}

	public DataList getNetBooks( NetBook vo ) throws Exception { 
		return netBookDao.getNetBooks ( vo ) ; 
	}
	
	public List< NetBook > getNetBooks4( NetBook vo ) throws Exception {
		return netBookDao.getNetBooks4 ( vo ) ;
	}
	
	public Long countgetNetBooks( NetBook vo ) throws Exception { 
		return netBookDao.countgetNetBooks ( vo ) ;
	}
	
	public List< NetBook > getNetBooks4Manager( NetBook vo ) throws Exception {
		return netBookDao.getNetBooks4Manager ( vo ) ;
	}	
	
	public Long countgetNetBooks4View( NetBook vo ) throws Exception { 
		return netBookDao.countgetNetBooks4View ( vo ) ;
	}
	
	public List< NetBook > getNetBookList( NetBook vo ) throws Exception {
		return netBookDao.getNetBookList ( vo ) ; 
	}
	
	public List< NetBook  > getNetBooksHasOffTime( NetBook vo ) throws Exception {
		return netBookDao.getNetBooksHasOffTime ( vo ) ; 
	}
	
	public NetBook getNetBookById( Long id ) throws Exception {
		return netBookDao.getNetBookById ( id ) ; 
	}
	
	
	public Long saveNetBook ( NetBook vo ) throws Exception {
		Long id = netBookDao.saveNetBook ( vo );
		vo.setTjOrder ( id );
		updateNetBook ( vo );
		return id;
	}
	
	public void updateNetBook( NetBook vo ) throws Exception {
		netBookDao.updateNetBook ( vo ) ; 
		
		NetBook cp = getNetBookById( vo.getProId ( ) ) ; 
		if ( cp != null ) {
			if ( cp.getProOnOffStatus ( ) == Const.PRO_PUB_STATUS_PUBED ) {
				SearchVo svo = getNetBook4Compass ( cp.getProId ( ) ) ; 
				if ( svo != null ) 
					searchCompass.setPro  ( cp.getProId ( ) ,  Const.PRO_TYPE_NETBOOK  , svo ) ; 
			}
			else 
				searchCompass.removePro  ( cp.getProId ( ) , Const.PRO_TYPE_NETBOOK ) ; 				
		}		
	}
		
	public void updateNetBookProOnOffStatus( NetBook cp ) throws Exception { 
		netBookDao.updateNetBookProOnOffStatus ( cp ) ;
		
		if ( cp != null ) {
			if ( cp.getProOnOffStatus ( ) == Const.PRO_PUB_STATUS_PUBED ) {
				SearchVo svo = getNetBook4Compass ( cp.getProId ( ) ) ; 
				if ( svo != null ) 
					searchCompass.setPro  ( cp.getProId ( ) ,  Const.PRO_TYPE_NETBOOK  , svo ) ; 
			}
			else 
				searchCompass.removePro  ( cp.getProId ( ) , Const.PRO_TYPE_NETBOOK ) ; 				
		}				
	}
	
	public void removeNetBook( Long id ) throws Exception{
		//delete compass
		SearchVo svo = getNetBook4Compass ( id ) ; 
		if ( svo != null )
			searchCompass.removePro  ( id , Const.PRO_TYPE_NETBOOK ) ; 

		
		netBookDao.removeNetBook ( id ) ;
		//
		Context.removeProRel ( id , Const.PRO_TYPE_NETBOOK )		 ;
	}
	
	//4 1
	public DataList getNetBooks4View( NetBook vo ) throws Exception {  
		return netBookDao.getNetBooks4View ( vo ) ; 
	}
	
	
	
	public NetBook getBaseNetBookInfoById4View ( Long id ) throws Exception {
		return getBaseNetBookInfoById4View ( id , Const.PRO_PUB_STATUS_PUBED );
	}
	
	public NetBook getBaseNetBookInfoById4View ( Long id , Byte proOnOffStatus )
			throws Exception {
		NetBook vo = new NetBook ( );
		vo.setProId ( id );
		vo.setProOnOffStatus ( proOnOffStatus );
		return netBookDao.getBaseNetBookInfoById4View ( vo );
	}
	//interface 
	//interface
	public NetBook getOrderNetBook( Long proId , Byte color , Long proServiceId , String COUNTRY  ) throws Exception {
		Assert.notNull ( proId  ) ; 
		Assert.notNull ( color  ) ; 
		Assert.notNull ( proServiceId  ) ; 
		Assert.notNull ( COUNTRY  ) ; 
		NetBook netBook = getBaseNetBookInfoById4View( proId , null ) ; 
		if ( netBook == null ) return null;
		/*
		if ( netBook.getProOnOffStatus ( ) != Const.PRO_PUB_STATUS_PUBED )
			return null; */
		List < ProImage > proImages = Context
				.getProImageServiceImpl ( )
				.getProImageList ( 
						new ProImage ( proId , Const.PRO_TYPE_NETBOOK , color ) );

		netBook.setProImages ( proImages ) ;
		
		ProService orderProService = Context.getProServiceServiceImpl ( ).getProServiceRelById( proServiceId , COUNTRY ) ;
		if ( orderProService == null ) return null ; 
		netBook.setOrderProService ( orderProService ) ;
		return netBook ; 
	}	
	
	public SearchVo getNetBook4Compass( Long proId ) {
		return netBookDao.getNetBook4Compass ( proId ) ;
	}
}
